package com.wisdytech.linkdcs.system.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.wisdytech.common.constants.CommonConstants;
import com.wisdytech.common.enums.AlarmType;
import com.wisdytech.common.enums.EquipmentStatus;
import com.wisdytech.common.handler.BaseService;
import com.wisdytech.common.utils.BaseUtil;
import com.wisdytech.common.utils.DateUtil;
import com.wisdytech.common.utils.MD5Util;
import com.wisdytech.linkdcs.energy.dao.IEqEnergyRecordDao;
import com.wisdytech.linkdcs.equipment.dao.IEqAlarmRecordDao;
import com.wisdytech.linkdcs.equipment.dao.IEqAutoAlarmRecordDao;
import com.wisdytech.linkdcs.equipment.dao.IEqEquipmentDao;
import com.wisdytech.linkdcs.equipment.dao.IEqStatusDao;
import com.wisdytech.linkdcs.equipment.model.AlarmRecord;
import com.wisdytech.linkdcs.equipment.model.Equipment;
import com.wisdytech.linkdcs.system.dao.ISysResourceDao;
import com.wisdytech.linkdcs.system.model.SysResource;
import com.wisdytech.linkdcs.system.model.SysUser;
import com.wisdytech.linkdcs.system.service.ILoginService;
import com.wisdytech.linkdcs.system.service.ISystemUserService;
import com.wisdytech.linkdcs.system.vo.IndexViewVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service("loginService")
@Transactional
public class LoginServiceImpl extends BaseService<ISysResourceDao, SysResource> implements ILoginService {

    @Resource(name="systemUserService")
    private ISystemUserService systemUserService;
    
    
    @Resource
    private IEqEquipmentDao equipmentDao;
    
    @Resource
    private IEqStatusDao statusDao;
    
    @Resource
    private IEqEnergyRecordDao energyRecordDao;
    
    @Resource
    private IEqAutoAlarmRecordDao autoAlarmRecordDao;
    
    @Resource
    private IEqAlarmRecordDao alarmRecordDao;
    
    
    
    /**
     * 缓存的首页对象
     */
    public IndexViewVO indexView=new IndexViewVO();
    
    
//    @Override
//    public LoginResult checkLoginUser(String userName,String loginPassword) {
//        SysUser user =systemUserService.getUserByName(userName);
//        //用户不存在
//        if(null == user){
//            return LoginResult.LOGINNAMENOTFOUND;
//        }
//
//        String pass = MD5Util.getDigest(loginPassword);
//        //密码错误
//        if (!pass.equals(user.getPassword())) {
//          return LoginResult.ERRORPASSWORD;
//        }
//
//        return LoginResult.LOGINOK;
//    }


	@Override
	public synchronized IndexViewVO getIndexViewVO() {
		
		//数据更新时间
		Date updateDate =indexView.getUpdateDate();
		//判断十分钟更新一次
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.MINUTE, -10);
		if(updateDate!=null && updateDate.after(calendar.getTime())){
			
			EntityWrapper<Equipment> ew =new EntityWrapper<>();
			ew.eq("DEL_FLAG", CommonConstants.DEL_FLAG);
			Integer count =equipmentDao.selectCount(ew);
			indexView.setNum(count);
			
			return indexView;
		}
		
		return getRealIndexViewVO();
	}
	
	/**
	 * 获取实时数据
	 * @return
	 */
	private IndexViewVO getRealIndexViewVO(){
		
		IndexViewVO viewVO =new IndexViewVO();
		
		
		
		//运行总时长
		Map<String,Object> countParams=new HashMap<>();
		countParams.put("eqStatus", EquipmentStatus.NORMAL.getCode());
        //统计时长 单位秒
        Long normalTimeCount =statusDao.countEqStopTime(countParams);
        viewVO.setRunTime(BaseUtil.divide(normalTimeCount, 3600L, 2));
        
		
		
		//停机总时长
        Map<String,Object> stopParams=new HashMap<>();
        stopParams.put("eqStatus", EquipmentStatus.UNUSUAL.getCode());
        //单位秒
        Long stopCount =statusDao.countEqStopTime(stopParams);
        viewVO.setStopTime(BaseUtil.divide(stopCount, 3600L, 2));
        
		//用电量累计
        
        Double meterCount =
        		energyRecordDao.countEnergyTime(CommonConstants.ENERGY_METER);
        viewVO.setElectricity(BaseUtil.getDecimal(meterCount, 2));
        
		//用水量累计
        
        Double waterCount =
        		energyRecordDao.countEnergyTime(CommonConstants.ENERGY_METER);
        viewVO.setWater(BaseUtil.getDecimal(waterCount, 2));
        
        
        
		//设备运行率
        //设备总数
        EntityWrapper<Equipment> ew =new EntityWrapper<>();
		ew.eq("DEL_FLAG", CommonConstants.DEL_FLAG);
		Integer count =equipmentDao.selectCount(ew);
		viewVO.setNum(count);
		
		
		if(count ==null || count == 0){
			viewVO.setOperatingRate(0.00);
		}else{
			//运行的设备
			ew.eq("REAL_STATUS", EquipmentStatus.NORMAL.getCode());
			Integer normalCount =equipmentDao.selectCount(ew);
			
			viewVO.setOperatingRate(BaseUtil.divide(normalCount, count, 2));
		}
		
		
		
		
        
        
		//设备告警率
		
		//统计当前告警设备
		Integer alaramEq =autoAlarmRecordDao.countAutoAlarmEq(DateUtil.getTodayTimeBegin());
		
		viewVO.setAlarmRate(BaseUtil.divide(alaramEq, count, 2));
		
		//预警修复率
		
		//预警总数
		EntityWrapper<AlarmRecord> alarmEw =new EntityWrapper<>();
		alarmEw.between("CREATE_DATE", DateUtil.getTodayTimeBegin(), new Date());
		int alarmCount =alarmRecordDao.selectCount(alarmEw);
//		normalEw.eq("STATUS", CommonConstants.DEL_FLAG);
		if(alarmCount==0){
			//没有预警
			viewVO.setRepairRate(0.00);
			
		}else{
			alarmEw.eq("STATUS", AlarmType.end.getCode());
			int endAlarmCount=alarmRecordDao.selectCount(alarmEw);
			viewVO.setRepairRate(BaseUtil.divide(endAlarmCount, alarmCount, 2));
		}
		
		viewVO.setUpdateDate(new Date());
		this.indexView=viewVO;
		return viewVO;
	}
    
}
